<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Models\User;
use App\Transformers\UserTransformer;
use App\Http\Requests\UserRequest;
use App\Models\Image;

class UsersController extends Controller
{
   public function me()
   {
       return $this->response->item($this->user(),new UserTransformer())
        ->setStatusCode(201);
   }

   public function update(UserRequest $request)
   {
        $user = $this->user();

        $attributes = $request->only(['name', 'email', 'introduction']);

        if (isset($request->avatar_image_id) && $request->avatar_image_id) {
            $image = Image::find($request->avatar_image_id);

            $attributes['avatar'] = $image->path;
        }else{
            $image = Image::query()->where('user_id',$user->id)->first();

            $attributes['avatar'] = $image->path;
        }
        $user->update($attributes);

        return $this->response->item($user, new UserTransformer());
   }

}
